package com.zx.oa.controller;

import com.zx.oa.dao.ClassInfo.ClassInfoDao;
import com.zx.oa.dao.ClassInfo.ClassInfoDaoImpl;
import com.zx.oa.dao.marketClass.MarketClassDao;
import com.zx.oa.dao.marketClass.MarketClassDaoImpl;
import com.zx.oa.entity.ClassInfo;
import com.zx.oa.entity.MarketClass;
import com.zx.oa.service.marketClass.MarketClassService;
import com.zx.oa.service.marketClass.MarketClassServiceImpl;
import com.zx.oa.utils.PageSupport;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/MarketClassServlet.do")
public class MarketClassServlet extends HttpServlet {
    MarketClassService marketClassService=new MarketClassServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String caozuo = request.getParameter("param");
        if ("cha".equals(caozuo)){
            chaMarketClass(request,response);
        }else if ("add".equals(caozuo)){
            addMarketClass(request,response);
        }else if ("modify_cha".equals(caozuo)){
            modify_chaMarketClass(request,response);
        }else if ("modify_save".equals(caozuo)){
            modify_saveMarketClass(request,response);
        }else if ("delete".equals(caozuo)){
            deleteMarketClass(request,response);
        }else if ("delete_cha".equals(caozuo)){
            delete_chaMarketClass(request,response);
        }
    }

    private void delete_chaMarketClass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int classId = Integer.parseInt(request.getParameter("mkClassId"));
        int classIdCount=marketClassService.getMarketClassIdCount(classId);
        PrintWriter out= response.getWriter();
        out.print(classIdCount);
        out.flush();
        out.close();
        response.sendRedirect("MarketClassServlet.do?param=cha");
    }

    private void deleteMarketClass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int classId = Integer.parseInt(request.getParameter("mkClassId"));
        boolean flag=marketClassService.deleteMarketClass(classId);
        if (flag){
            response.sendRedirect("MarketClassServlet.do?param=cha");
        }
    }

    private void modify_saveMarketClass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String classType = request.getParameter("classType");
        String name = request.getParameter("className");
        int classId = Integer.parseInt(request.getParameter("mkClassId"));
        boolean flag=marketClassService.modifyMarketClass(name,classType,classId);
        if (flag){
            response.sendRedirect("MarketClassServlet.do?param=cha");
        }
    }

    private void modify_chaMarketClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int classId = Integer.parseInt(request.getParameter("mkClassId"));
        MarketClass marketClass=marketClassService.getMarketClassLists(classId);
        request.getSession().setAttribute("marketClass",marketClass);
        request.getRequestDispatcher("jsp/marketClassModify.jsp").forward(request, response);

    }

    private void addMarketClass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String classType = request.getParameter("classType");
        String className = request.getParameter("className");
        boolean flag=marketClassService.addMarketClass(className,classType);
        if (flag){
            response.sendRedirect("MarketClassServlet.do?param=cha");
        }
    }

    private void chaMarketClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String Type=null;
        String classType = request.getParameter("classType");
        if(null!=classType&&!"".equals(classType)){
            Type=classType;
        }
        String pageIndex= request.getParameter("pageIndex");
        if(pageIndex==null){
            //第一次访问查询的servlet的时候没有pageIndex，赋默认值1
            pageIndex="1";
        }
        int currentPageNo=1;//当前页号
        int pageSize= PageSupport.pageSizeVal;//页面尺寸复制
        currentPageNo=Integer.parseInt(pageIndex);

        List<MarketClass> marketClassList=marketClassService.getMarketClassList(Type,currentPageNo,pageSize);
        MarketClassDao marketClassDao = new MarketClassDaoImpl();
        int totalCount=marketClassDao.getClassInfoTotalCount(Type);

        //存储页面信息
        PageSupport pages=new PageSupport();
        //设置页面尺寸
        pages.setPageSize(pageSize);
        //设置页面总记录数（注意此方法计算得到总页面数量）
        pages.setTotalCount(totalCount);
        //获取到总的页数
        int totalPageCount=pages.getTotalPageCount();
        if(currentPageNo<1){
            currentPageNo=1;
        }else if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        //将当前页号存储到pages对象
        pages.setCurrentPageNo(currentPageNo);



        List<MarketClass> classTypeList=marketClassService.getMarketClassType();
        request.setAttribute("pages",pages);//分页信息
        request.setAttribute("classType",classType);
        request.getSession().setAttribute("classTypeList",classTypeList);
        request.getSession().setAttribute("marketClassList",marketClassList);
        request.getRequestDispatcher("jsp/marketClass.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
